package com.lc.pianostudent.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.lc.pianostudent.R;


/**
 * 英文字母A-Z过滤控件
 * @author Administrator
 *
 */
public class SideBar extends View {


	private static final String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
	private int choose = -1;
	private Paint paint = new Paint();
	private boolean showBg = false;
	private OnLetterChangedListener onLetterChangedListener;
	private TextView overlay;

	private Paint mPaint;

	private int radial = 21;


	public SideBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public SideBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SideBar(Context context) {
		super(context);
	}

	/**
	 * 设置悬浮的textview
	 * @param overlay
	 */
	public void setOverlay(TextView overlay){
		this.overlay = overlay;
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (showBg) {
			canvas.drawColor(Color.TRANSPARENT);
		}

		int height = getHeight();
		int width = getWidth();
		int singleHeight = height / b.length;
		int pingmu_width = getScreenWidth();
		for (int i = 0; i < b.length; i++) {
			if(pingmu_width == 720){
				paint.setTextSize(pingmu_width/30);
			}else if(pingmu_width==768){
				paint.setTextSize(pingmu_width/32);
			}else if(pingmu_width > 768  && pingmu_width <= 1080){
				paint.setTextSize(pingmu_width/30);
			}else if(pingmu_width>1080){
				paint.setTextSize(pingmu_width/40);
			}else if(pingmu_width<720){
				paint.setTextSize(pingmu_width/25);
			}

			paint.setColor(getResources().getColor(R.color.test));
			paint.setAntiAlias(true);

			mPaint = new Paint();
			// 设置画笔为抗锯齿
			mPaint.setAntiAlias(true);
			// 设置颜色为红色
			mPaint.setColor(Color.TRANSPARENT);
			/**
			 * 画笔样式分三种： 1.Paint.Style.STROKE：描边 2.Paint.Style.FILL_AND_STROKE：描边并填充
			 * 3.Paint.Style.FILL：填充
			 */
			mPaint.setStyle(Paint.Style.FILL);
			/**
			 * 设置描边的粗细，单位：像素px 注意：当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素
			 */
			mPaint.setStrokeWidth(20);

			if (i == choose) {
				mPaint.setColor(getResources().getColor(R.color.textBlack_bfbfbf));
				paint.setColor(getResources().getColor(R.color.colorWhite));
				  //加粗
			}
			float xPos = width / 2 - paint.measureText(b[i]) / 2;
			float yPos = singleHeight * i + singleHeight;

			if(pingmu_width == 720){
				canvas.drawCircle((width / 2)+50, (float) (singleHeight * i+ (singleHeight/1.4)), pingmu_width/50, mPaint);

			}else if(pingmu_width==768){
				canvas.drawCircle((width / 2)+50, (float) (singleHeight * i+ (singleHeight/1.4)), pingmu_width/55, mPaint);

			}else if(pingmu_width > 768  && pingmu_width <= 1080){
				canvas.drawCircle((width / 2)+50, (float) (singleHeight * i+ (singleHeight/1.4)), pingmu_width/55, mPaint);

			}else if(pingmu_width>1080){
				canvas.drawCircle((width / 2)+50, (float) (singleHeight * i+ (singleHeight/1.4)), pingmu_width/70, mPaint);

			}else if(pingmu_width<720){
				canvas.drawCircle((width / 2)+50, (float) (singleHeight * i+ (singleHeight/1.4)), pingmu_width/40, mPaint);

			}
			canvas.drawText(b[i], xPos+50, yPos, paint);
			mPaint.reset();
			paint.reset();
		}

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = choose;
		final OnLetterChangedListener listener = onLetterChangedListener;
		final int c = (int) (y / getHeight() * b.length);

		switch (action) {
			case MotionEvent.ACTION_DOWN:
				showBg = true;
				if (oldChoose != c && listener != null) {
					if (c >= 0 && c < b.length) {
						listener.onLetterChanged(b[c]);
						choose = c;
						invalidate();
						if (overlay != null){
							overlay.setVisibility(VISIBLE);
							overlay.setText(b[c]);
						}
					}
				}

				break;
			case MotionEvent.ACTION_MOVE:
				if (oldChoose != c && listener != null) {
					if (c >= 0 && c < b.length) {
						listener.onLetterChanged(b[c]);
						choose = c;
						invalidate();
						if (overlay != null){
							overlay.setVisibility(VISIBLE);
							overlay.setText(b[c]);
						}
					}
				}
				break;
			case MotionEvent.ACTION_UP:
				showBg = false;
//				showBg = true;
				choose = -1;
//				choose = c;
				invalidate();
				if (overlay != null){
					overlay.setVisibility(GONE);
				}
				break;
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	public void setOnLetterChangedListener(OnLetterChangedListener onLetterChangedListener) {
		this.onLetterChangedListener = onLetterChangedListener;
	}

	public interface OnLetterChangedListener {
		void onLetterChanged(String letter);
	}
	private int getScreenWidth() {
		WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics displayMetrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(displayMetrics);
		return displayMetrics.widthPixels;
	}
}
